package com.lbloca.locate.ui.fragment;

import android.Manifest;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;
import com.lbloca.locate.R;
import com.lbloca.locate.application.LoApplication;
import com.lbloca.locate.beans.FriendBean;
import com.lbloca.locate.biz.AppBiz;
import com.lbloca.locate.biz.UserBiz;
import com.lbloca.locate.ui.activity.AddAttentionPeopleActivity;
import com.lbloca.locate.ui.activity.FriendLoactionActivity;
import com.lbloca.locate.ui.activity.MainActivity;
import com.lbloca.locate.ui.activity.VipActivity;
import com.lbloca.locate.ui.base.BaseImmersionBarFragment;
import com.lbloca.locate.utils.AppUtils;
import com.lbloca.locate.utils.GsonUtil;
import com.lbloca.locate.utils.LogUtils;
import com.lbloca.locate.utils.PermissionUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.http.okhttp.callback.StringCallback;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

public class AttentionFragment extends BaseImmersionBarFragment {
    private static final String TAG = "AttentionFragment";
    @BindView(R.id.id_rl_add_people)
    RelativeLayout mRlAddPeople;

    @BindView(R.id.id_rv_friend)
    RecyclerView mRvFriend;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mSrLayout;


    private MyLocationConfiguration mLocationConfiguration;
    public BDLocation mLocation;
    private MultiItemTypeAdapter mFriendAdapter;

    private List<FriendBean> mFriendList = new ArrayList<>();

    public static final AttentionFragment getInstance() {
        return new AttentionFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.frag_attention;
    }

    @Override
    protected void initView(View view) {
        initImmersionBar();

        mFriendList.add(new FriendBean());


        mFriendAdapter= new MultiItemTypeAdapter(mContext,mFriendList);
        mFriendAdapter.addItemViewDelegate(new MeLocationItemDelagate());
        mFriendAdapter.addItemViewDelegate(new FriendLocationItemDelagate());

        mRvFriend.setAdapter(mFriendAdapter);
        mSrLayout.setEnableAutoLoadMore(true);

        mSrLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                LogUtils.i(TAG,"---onLoadMore---");

                mSrLayout.finishLoadMoreWithNoMoreData();
            }
        });
        mSrLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {


                refreshFriendData();
            }
        });



    }





    @Override
    protected void initData() {
        register("");
    }


    @Override
    protected void initEvent() {
        mFriendAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                if(i==0){
                    ((MainActivity)getActivity()).goLocationPage();
                }else if(i>0){
                    FriendLoactionActivity.start(mContext,mFriendList.get(i));
                }

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });
    }

    @OnClick({R.id.id_rl_add_people,R.id.id_iv_add_attention})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_iv_add_attention:
            case R.id.id_rl_add_people:
                boolean isVip = AppBiz.getInstance().getVipState();
                if(isVip){
                    AddAttentionPeopleActivity.start(mContext);
                }else{
                    VipActivity.start(mContext);
                }

                break;
            default:
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mSrLayout.autoRefresh();
        }
    }

    private void register(String phone) {
        AppBiz.getInstance().register(phone, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                if (!TextUtils.isEmpty(response) && response.contains("id")) {
                    UserBiz.getInstance().setUserInfo(response);
                    AppBiz.getInstance().getFriends(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                        }

                        @Override
                        public void onResponse(String response, int id) {
                            LogUtils.i(TAG,response);
                            if (!TextUtils.isEmpty(response)) {
                                mFriendList.addAll(GsonUtil.json2List(response, new TypeToken<List<FriendBean>>() {}));
                                mFriendAdapter.notifyDataSetChanged();
                            }
                        }
                    });


                }
            }
        });
    }


    public class MeLocationItemDelagate implements ItemViewDelegate<FriendBean>
    {

        @Override
        public int getItemViewLayoutId()
        {
            return R.layout.vw_home_friend_item;
        }

        @Override
        public boolean isForViewType(FriendBean item, int position) {
            if(position==0){
                return true;
            }
            return false;
        }

        @Override
        public void convert(ViewHolder holder, FriendBean friendBean, int position) {

            if (mLocation != null ) {
                holder.setText(R.id.id_tv_location_address,mLocation.getAddrStr());
            }else{
                holder.setText(R.id.id_tv_location_address,"---");
            }
            holder.setText(R.id.id_tv_time,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            holder.setText(R.id.id_tv_name,"我");
        }
    }

    public class FriendLocationItemDelagate implements ItemViewDelegate<FriendBean>
    {

        @Override
        public int getItemViewLayoutId()
        {
            return R.layout.vw_home_friend_item;
        }

        @Override
        public boolean isForViewType(FriendBean item, int position) {
            return position>0;
        }

        @Override
        public void convert(ViewHolder holder, FriendBean friendBean, int position) {
            holder.setText(R.id.id_tv_location_address,friendBean.getAddress());
            holder.setText(R.id.id_tv_time,friendBean.getUpdatedate());
            holder.setText(R.id.id_tv_name,friendBean.getPhone()+"");
        }
    }

    private void refreshFriendData(){
        AppBiz.getInstance().getFriends(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                mSrLayout.finishRefresh();
            }

            @Override
            public void onResponse(String response, int id) {
                if (!TextUtils.isEmpty(response)) {
                    mFriendList.clear();
                    mFriendList.add(new FriendBean());
                    mFriendList.addAll(GsonUtil.json2List(response, new TypeToken<List<FriendBean>>() {}));
                    mFriendAdapter.notifyDataSetChanged();
                }
                mSrLayout.finishRefresh();
            }
        });
    }




    public void changeMap() {
        if (mLocation != null ) {
            mFriendAdapter.notifyItemChanged(0);
        }
    }


    public void initImmersionBar() {
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .navigationBarColor(R.color.colorPrimary)
                .init();
    }
}
